from typing import Callable
from fastapi import FastAPI

from app.db.mysql import init_mysql
from app.db.redis import sys_cache
from app.core.Logging import setup_logging

"""
    启动配置
"""

def startup(app: FastAPI) -> Callable:
    """
    FastApi 启动完成事件
    :param app: FastAPI
    :return: start_app
    """
    async def app_start() -> None:
        setup_logging()
        # APP启动完成后触发
        print("启动完毕")
        # 注册数据库
        await init_mysql(app)
        # 注入缓存到app state
        app.state.cache= await sys_cache() # type: ignore
    return app_start


def stopping(app: FastAPI) -> Callable:
    """
    FastApi 停止事件
    :param app: FastAPI
    :return: stop_app
    """
    async def stop_app() -> None:
        # APP停止时触发
        print("停止")
        pass

    return stop_app
